package com.cos.openapi.services.impl;

import com.cos.openapi.dao.CustomerMapper;
import com.cos.openapi.entity.Customer;
import com.cos.openapi.pojo.JsonResult;
import com.cos.openapi.pojo.TableResult;
import com.cos.openapi.services.CustomerServices;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: COS
 * @time: 2021/2/1 9:13
 * @description:
 */
@Service
public class CustomerServicesImpl implements CustomerServices {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public TableResult getCustomerByNameAndState(Integer page, Integer limit, String name, Integer state) {
        //找分页工具
        PageHelper.startPage(page, limit);
        //请求dao获取数据
        List<Customer> customerList = customerMapper.getCustomerByNameAndState(name, state);
        //使用pageInfo帮忙分装数据
        PageInfo<Customer> info = new PageInfo<>(customerList);
        //返回数据
        return TableResult.getResultBySuccess(info.getTotal(), info.getList());
    }

    @Override
    public JsonResult update(Customer customer) {
        if (customer != null) {
            int i = customerMapper.updateByPrimaryKeySelective(customer);
            if (i > 0) {
                return JsonResult.getJsonResultBySuccess("修改成功");
            }
        }
        return JsonResult.getJsonResultByFail("修改失败");
    }

    @Override
    public JsonResult add(Customer customer) {
        if (customer != null) {
            int i = customerMapper.insertSelective(customer);
            if (i > 0) {
                return JsonResult.getJsonResultBySuccess("新增成功");
            }
        }
        return JsonResult.getJsonResultByFail("新增失败");
    }

    @Override
    public JsonResult del(Integer[] ids) {
        if (ids!=null&&ids.length>0) {
            customerMapper.delCusotmer(ids);
            /*for (Integer id : ids) {

            }*/
            return JsonResult.getJsonResultBySuccess("删除成功");
        }
        return JsonResult.getJsonResultByFail("删除失败");
    }
}
